package com.ruoyi.supervise.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supervise.domain.BasePostProPersonCert;
import com.ruoyi.supervise.domain.BaseSpecialEquPersonCert;
import com.ruoyi.supervise.domain.BasicScoreContent;
import com.ruoyi.supervise.domain.CertChangeInfo;
import com.ruoyi.supervise.domain.CertRecheckInfo;
import com.ruoyi.supervise.domain.IGMPersonCreditRank;
import com.ruoyi.supervise.domain.IGMPersonalPoorInfo;
import com.ruoyi.supervise.domain.IGMPersonalWellInfo;
import com.ruoyi.supervise.domain.IntegrityPerformance;
import com.ruoyi.supervise.domain.PersonInfo;
import com.ruoyi.supervise.domain.ProTitlePersonCert;
import com.ruoyi.supervise.domain.RARegInfo;
import com.ruoyi.supervise.mapper.PersonInfoMapper;
import com.ruoyi.supervise.service.IPersonInfoService;
import com.ruoyi.supervise.util.XmlReadUtil;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

/**
 * @ClassName: PersonInfoImpl
 * @Description: 人员信息impl
 * @Author wtj
 * @DateTime 2020年3月25日 上午11:41:37
 */
@Service
@DataSource(value = DataSourceType.SLAVE)
public class PersonInfoImpl implements IPersonInfoService 
{
    @Autowired
    private PersonInfoMapper personInfoMapper;

	@Override
	public PersonInfo selectPersonInfoByGuid(String guid) {
		// TODO Auto-generated method stub
		return personInfoMapper.selectPersonInfoByGuid(guid);
	}

	@Override
	public List<RARegInfo> selectRegTypeByPersonBaseGuid(String personBaseGuid) {
		// TODO Auto-generated method stub
		return personInfoMapper.selectRegTypeByPersonBaseGuid(personBaseGuid);
	}

	@Override
	public List<ProTitlePersonCert> selectProTitlePersonCertByPersonBaseGuid(String personBaseGuid) {
		// TODO Auto-generated method stub
		return personInfoMapper.selectProTitlePersonCertByPersonBaseGuid(personBaseGuid);
	}

	
	@Override      
	public PersonInfo selectQYPersonInfoByGuid(String id) {
		PersonInfo person = new PersonInfo();
		if(!StringUtils.isEmpty(id)) {
			person = personInfoMapper.selectPersonInfoByGuid(id);
			   if(person != null) {
				   List<RARegInfo> rARegInfoList=personInfoMapper.selectRegTypeByPersonBaseGuid(person.getGuid());
				   if(!StringUtils.isEmpty(rARegInfoList)) {
					   String strRegType = "0";
					   for(RARegInfo rag:rARegInfoList) {
						   if(strRegType.equals("0")) {
							   strRegType = rag.getRegType();
						   }else {
							   strRegType = strRegType + " " + rag.getRegType();
						   }
					   }
					   person.setQualication(strRegType);
				   }
				   
				   List<ProTitlePersonCert> ppcList=personInfoMapper.selectProTitlePersonCertByPersonBaseGuid(person.getGuid());
				   if(!StringUtils.isEmpty(ppcList)) {
					   String strPpc = "0";
					   for(ProTitlePersonCert ppc:ppcList) {
						   if(strPpc.equals("0")) {
							   strPpc = ppc.getQualName();
						   }else {
							   strPpc = strPpc + " " + ppc.getQualName();
						   }
					   }
					   person.setProTechQual(strPpc);
				   }
				   //mmap.put("legalPerson", legalPerson);
				   
			   }else {
				   //mmap.put("legalPerson", null); 
				   person = null;
			   }
		   }else {
			   //mmap.put("legalPerson", null);
			   person = null;
		   }
		return person;
	}

	@Override
	public List<PersonInfo> selectBasePersonInfoByCompanyGuid(PersonInfo personInfo) {
		List<PersonInfo> piList = personInfoMapper.selectBasePersonInfoByCompanyGuid(personInfo);
		if(!StringUtils.isEmpty(piList)) {
			for(PersonInfo pi:piList) {
				if(!StringUtils.isEmpty(pi.getPersonType())) {
					if(StrUtil.containsAny(pi.getPersonType(), "|")) {
						String pt="";
						String[] subStr=StrUtil.split(pi.getPersonType(), "|");
						for(String s:subStr) {
							if(pt == "" && !StringUtils.isEmpty(s)) {
								pt = s;
							}else if(pt != ""){
								pt = pt + "、" + s;
							}
						}
						pi.setPersonType(pt);
					}
				}
			}
		}
		return piList;
	}

	@Override
	public List<RARegInfo> selectBaseRARegInfoByPersonBaseGuid(String personBaseGuid) {
		List<RARegInfo> rarList = personInfoMapper.selectBaseRARegInfoByPersonBaseGuid(personBaseGuid);
		if(!StringUtils.isEmpty(rarList)) {
			for(RARegInfo rar:rarList) {
				if(!StringUtils.isEmpty(rar.getEffectDate())) {
					//过期是false
					if(!DateUtil.isExpired(DateUtil.parseDate(rar.getEffectDate()), DateField.MILLISECOND, 0, new Date())) {
						rar.setQState("有效");
					}else {
						rar.setQState("无效");
					}
				}else {
					rar.setQState("无效");
				}
				if(!StringUtils.isEmpty(rar.getCertImage())) {
					String[] subStr=StrUtil.split(rar.getCertImage(), "|");
					if(subStr.length >= 2) {
						rar.setCertImage(Global.getConfig("ruoyi.bigPlatform")+subStr[1]);
					}
				}
				rar.setRARegMajInfoEntity(personInfoMapper.selectBaseRARegMajInfoByRARegInfoGuid(rar.getGuid()));
				if(!StringUtils.isEmpty(rar.getCertChangeInfo())) {
					CertChangeInfo cci = new CertChangeInfo();
					cci.setGuid("Guid");
					cci.setType("Type");
					cci.setCreateDate("CreateDate");
					cci.setContent("Content");
					cci.setPersonCertificateBillGuid("PersonCertificateBillGuid");
					String nodePath = "/PsDataPacket/FDataList/ModifyInfo";
					List<CertChangeInfo> cciList = XmlReadUtil.getCertChangeInfoXml(rar.getCertChangeInfo(), nodePath, cci);
					rar.setCertChangeInfoEntity(cciList);
				}
			}
		}
		return rarList;
	}

	@Override
	public List<ProTitlePersonCert> selectBaseProTitlePersonCertByPersonBaseGuid(String personBaseGuid) {
		List<ProTitlePersonCert> ptpcList = personInfoMapper.selectBaseProTitlePersonCertByPersonBaseGuid(personBaseGuid);
		if(!StringUtils.isEmpty(ptpcList)) {
			for(ProTitlePersonCert ptpc:ptpcList) {
				if(!StringUtils.isEmpty(ptpc.getCertImage())) {
					String[] subStr=StrUtil.split(ptpc.getCertImage(), "|");
					if(subStr.length >= 2) {
						ptpc.setCertImage(Global.getConfig("ruoyi.bigPlatform")+subStr[1]);
					}
				}
				if(!StringUtils.isEmpty(ptpc.getCertChangeInfo())) {
					CertChangeInfo cci = new CertChangeInfo();
					cci.setGuid("Guid");
					cci.setType("Type");
					cci.setCreateDate("CreateDate");
					cci.setContent("Content");
					cci.setPersonCertificateBillGuid("PersonCertificateBillGuid");
					String nodePath = "/PsDataPacket/FDataList/ModifyInfo";
					ptpc.setCertChangeInfoEntity(XmlReadUtil.getCertChangeInfoXml(ptpc.getCertChangeInfo(), nodePath, cci));
				}
				if(!StringUtils.isEmpty(ptpc.getCertRecheckInfo())) {
					CertRecheckInfo cri = new CertRecheckInfo();
					cri.setGuid("Guid");
					cri.setHours("Hours");
					cri.setRecheckDate("RecheckDate");
					String nodePath = "/PsDataPacket/FDataList/RecheckRecord";
					ptpc.setCertRecheckInfoEntity(XmlReadUtil.getCertRecheckInfoXml(ptpc.getCertRecheckInfo(), nodePath, cri));
				}
			}
		}
		return ptpcList;
	}

	@Override
	public List<BasePostProPersonCert> selectBasePostProPersonCertByPersonBaseGuid(String personBaseGuid) {
		List<BasePostProPersonCert> bpppcList = personInfoMapper.selectBasePostProPersonCertByPersonBaseGuid(personBaseGuid);
		if(!StringUtils.isEmpty(bpppcList)) {
			for(BasePostProPersonCert bpppc:bpppcList) {
				if(!StringUtils.isEmpty(bpppc.getCertImage())) {
					String[] subStr=StrUtil.split(bpppc.getCertImage(), "|");
					if(subStr.length >= 2) {
						bpppc.setCertImage(Global.getConfig("ruoyi.bigPlatform")+subStr[1]);
					}
				}
				if(!StringUtils.isEmpty(bpppc.getCertChangeInfo())) {
					CertChangeInfo cci = new CertChangeInfo();
					cci.setGuid("Guid");
					cci.setType("Type");
					cci.setCreateDate("CreateDate");
					cci.setContent("Content");
					cci.setPersonCertificateBillGuid("PersonCertificateBillGuid");
					String nodePath = "/PsDataPacket/FDataList/ModifyInfo";
					bpppc.setCertChangeInfoEntity(XmlReadUtil.getCertChangeInfoXml(bpppc.getCertChangeInfo(), nodePath, cci));
				}
				if(!StringUtils.isEmpty(bpppc.getCertRecheckInfo())) {
					CertRecheckInfo cri = new CertRecheckInfo();
					cri.setGuid("Guid");
					cri.setHours("Hours");
					cri.setRecheckDate("RecheckDate");
					String nodePath = "/PsDataPacket/FDataList/RecheckRecord";
					bpppc.setCertRecheckInfoEntity(XmlReadUtil.getCertRecheckInfoXml(bpppc.getCertRecheckInfo(), nodePath, cri));
				}
			}
		}
		return bpppcList;
	}

	@Override
	public List<BaseSpecialEquPersonCert> selectBaseSpecialEquPersonCertByPersonBaseGuid(String personBaseGuid) {
		List<BaseSpecialEquPersonCert> bsepcList = personInfoMapper.selectBaseSpecialEquPersonCertByPersonBaseGuid(personBaseGuid);
		if(!StringUtils.isEmpty(bsepcList)) {
			for(BaseSpecialEquPersonCert bsepc:bsepcList) {
				if(!StringUtils.isEmpty(bsepc.getCertImage())) {
					String[] subStr=StrUtil.split(bsepc.getCertImage(), "|");
					if(subStr.length >= 2) {
						bsepc.setCertImage(Global.getConfig("ruoyi.bigPlatform")+subStr[1]);
					}
				}
				if(!StringUtils.isEmpty(bsepc.getCertChangeInfo())) {
					CertChangeInfo cci = new CertChangeInfo();
					cci.setGuid("Guid");
					cci.setType("Type");
					cci.setCreateDate("CreateDate");
					cci.setContent("Content");
					cci.setPersonCertificateBillGuid("PersonCertificateBillGuid");
					String nodePath = "/PsDataPacket/FDataList/ModifyInfo";
					bsepc.setCertChangeInfoEntity(XmlReadUtil.getCertChangeInfoXml(bsepc.getCertChangeInfo(), nodePath, cci));
				}
				if(!StringUtils.isEmpty(bsepc.getCertRecheckInfo())) {
					CertRecheckInfo cri = new CertRecheckInfo();
					cri.setGuid("Guid");
					cri.setHours("Hours");
					cri.setRecheckDate("RecheckDate");
					String nodePath = "/PsDataPacket/FDataList/RecheckRecord";
					bsepc.setCertRecheckInfoEntity(XmlReadUtil.getCertRecheckInfoXml(bsepc.getCertRecheckInfo(), nodePath, cri));
				}
			}
		}
		return bsepcList;
	}

	@Override
	public List<IntegrityPerformance> selectIntegrityPerformanceListByPeopleIdCard(IntegrityPerformance ip) {
		// TODO Auto-generated method stub
		return personInfoMapper.selectIntegrityPerformanceListByPeopleIdCard(ip);
	}

	@Override
	public List<IGMPersonalWellInfo> selectIGMPersonalWellInfoListByPersonGuid(IGMPersonalWellInfo igmpwi) {
		// TODO Auto-generated method stub
		return personInfoMapper.selectIGMPersonalWellInfoListByPersonGuid(igmpwi);
	}

	@Override
	public IGMPersonalWellInfo selectIGMPersonalWellInfoByGuid(String guid) {
		IGMPersonalWellInfo igmpwi = personInfoMapper.selectIGMPersonalWellInfoByGuid(guid);
		if(igmpwi!=null) {
			String[] subMyStr=StrUtil.split(igmpwi.getMaterials(), "|");
			if(subMyStr.length == 4) {
				String[] file = StrUtil.split(subMyStr[1], "/");
				if(file.length >= 3) {
					if("profile".equals(file[1]) && "lingshui".equals(file[2]) && StrUtil.containsAny(subMyStr[1], "/profile/lingshui/")) {
	            		igmpwi.setFileSort("1");
	            		igmpwi.setMaterials(subMyStr[1]);
	            		igmpwi.setFileName(subMyStr[2]);
	            	}else {
	            		igmpwi.setFileSort("2");
	            		igmpwi.setMaterials("");
	            	}
				}else if(StrUtil.containsAny(subMyStr[1],"\\")){
					if(StrUtil.split(subMyStr[1], "\\").length == 4) {
						igmpwi.setFileSort("0");
						igmpwi.setMaterials(Global.getConfig("ruoyi.bigPlatform") + subMyStr[1]);
					}else {
						igmpwi.setFileSort("2");
	            		igmpwi.setMaterials("");
					}
				}
			}else {
				igmpwi.setFileSort("2");
        		igmpwi.setMaterials("");
			}
		}
		return igmpwi;
	}

	@Override
	public List<IGMPersonalPoorInfo> selectIGMPersonalPoorInfoListByPersonGuid(IGMPersonalPoorInfo igmppi) {
		// TODO Auto-generated method stub
		return personInfoMapper.selectIGMPersonalPoorInfoListByPersonGuid(igmppi);
	}

	@Override
	public IGMPersonalPoorInfo selectIGMPersonalPoorInfoByGuid(String guid) {
		IGMPersonalPoorInfo igmppi = personInfoMapper.selectIGMPersonalPoorInfoByGuid(guid);
		if(igmppi!=null) {
			String[] subMyStr=StrUtil.split(igmppi.getMaterials(), "|");
			if(subMyStr.length == 4) {
				String[] file = StrUtil.split(subMyStr[1], "/");
				if(file.length >= 3) {
					if("profile".equals(file[1]) && "lingshui".equals(file[2]) && StrUtil.containsAny(subMyStr[1], "/profile/lingshui/")) {
						igmppi.setFileSort("1");
						igmppi.setMaterials(subMyStr[1]);
						igmppi.setFileName(subMyStr[2]);
					}else {
						igmppi.setFileSort("2");
						igmppi.setMaterials("");
					}
				}else if(StrUtil.containsAny(subMyStr[1],"\\")) {
					if(StrUtil.split(subMyStr[1], "\\").length == 4) {
						igmppi.setFileSort("0");
						igmppi.setMaterials(Global.getConfig("ruoyi.bigPlatform") + subMyStr[1]);
					}else {
						igmppi.setFileSort("2");
						igmppi.setMaterials("");
					}
				}
			}else {
				igmppi.setFileSort("2");
				igmppi.setMaterials("");
			}
			if(subMyStr.length >= 2) {
				igmppi.setMaterials(Global.getConfig("ruoyi.bigPlatform")+subMyStr[1]);
			}else {
				igmppi.setMaterials(null);
			}
		}
		return igmppi;
	}

	@Override
	public List<IGMPersonCreditRank> selectIGMPersonCreditRankByPersonGuid(String personGuid) {
		List<IGMPersonCreditRank> igmpcrList = personInfoMapper.selectIGMPersonCreditRankByPersonGuid(personGuid);
		IGMPersonalWellInfo igmpwi = new IGMPersonalWellInfo();
		igmpwi.setPersonGuid(personGuid);
		List<IGMPersonalWellInfo> igmpwiList = personInfoMapper.selectIGMPersonalWellInfoListByPersonGuid(igmpwi);
		List<BasicScoreContent> personalWellInfoList = new ArrayList<BasicScoreContent>();
		if(!StringUtils.isEmpty(igmpwiList)) {
			for(IGMPersonalWellInfo pwi:igmpwiList) {
				BasicScoreContent personalWellInfo = new BasicScoreContent();
				personalWellInfo.setDate(pwi.getAwardDate());
				personalWellInfo.setContent(pwi.getBehaviorDescription());
				personalWellInfo.setScore(Double.valueOf(pwi.getScore()));
				personalWellInfoList.add(personalWellInfo);
			}
		}
		IGMPersonalPoorInfo igmppi = new IGMPersonalPoorInfo();
		igmppi.setPersonGuid(personGuid);
		List<IGMPersonalPoorInfo> igmppiList = personInfoMapper.selectIGMPersonalPoorInfoListByPersonGuid(igmppi);
		List<BasicScoreContent> personnalPoorInfoList = new ArrayList<BasicScoreContent>();
		if(!StringUtils.isEmpty(igmppiList)) {
			for(IGMPersonalPoorInfo ppi:igmppiList) {
				BasicScoreContent personnalPoorInfo = new BasicScoreContent();
				personnalPoorInfo.setDate(ppi.getPunishmentDate());
				personnalPoorInfo.setContent(ppi.getBehaviorDescription());
				personnalPoorInfo.setScore(Double.valueOf(ppi.getPenaltyScores()));
				personnalPoorInfoList.add(personnalPoorInfo);
			}
		}
		if(!StringUtils.isEmpty(igmpcrList)) {
			for(IGMPersonCreditRank igmpcr:igmpcrList) {
				List<BasicScoreContent> bscList = new ArrayList<BasicScoreContent>();
				String[] subStr=StrUtil.split(igmpcr.getBasicScoreContent(), "|");
				if(subStr.length!=0) {
					for(String ss:subStr) {
						String[] str=StrUtil.split(ss, ",");
						if(str.length==3) {
							BasicScoreContent bsc = new BasicScoreContent();
							bsc.setDate(str[0]);
							bsc.setContent(str[1]);
							bsc.setScore(Double.valueOf(str[2]));
							bscList.add(bsc);
						}
					}
				}
				igmpcr.setBasicScoreContentEntity(bscList);
				igmpcr.setPersonalWellInfoList(personalWellInfoList);
				igmpcr.setGoodScoreAdd(igmpcr.getGoodScore());
				igmpcr.setPersonnalPoorInfoList(personnalPoorInfoList);
				igmpcr.setBadScoreSub(100-igmpcr.getBadScore());
				if(igmpcr.getPost().equals("注册造价工程师")) {
					//基本信用分模块
					igmpcr.setBasicScore(igmpcr.getBasicScore()/0.3);
					//良好行为信息模块
					igmpcr.setGoodScore(igmpcr.getGoodScore()/0.2);
					//不良行为信息模块
					igmpcr.setBadScore(igmpcr.getBadScore()/0.5);
				}
			}
		}
		return igmpcrList;
	}

	@Override
	public List<PersonInfo> selectPersonInfoList(PersonInfo personInfo) {
		// TODO Auto-generated method stub
		return personInfoMapper.selectPersonInfoList(personInfo);
	}
}
